package com.example.jsonquynh;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Favorite extends Activity {
	private MenuSetting menu;
	private FavoriteManager favoriteManager;
	List<Place> placeList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		menu = new MenuSetting(this);
	}

	private void updateList() {
		placeList = favoriteManager.getAllFavoritePlaces();
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				ListView list = (ListView) findViewById(R.id.list);

				BinderData binderData = new BinderData(Favorite.this, placeList);
				list.setAdapter(binderData);
				list.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

						Intent i = new Intent();
						i.setClass(Favorite.this, DetailPlace.class);
						i.putExtra("place", placeList.get(position));
						startActivity(i);
					}
				});
			}
		});
	}

	@Override
	protected void onPause() {
		favoriteManager.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		menu.showContent();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if (favoriteManager == null ){favoriteManager = new FavoriteManager(Favorite.this);}
				favoriteManager.open();
				updateList();
				
			}
		}).start();
		
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
